package com.cwf.quality.common.strategy.analyzer;

import com.cwf.quality.common.enums.DataAnalyzerEnum;
import com.cwf.quality.model.dto.QualityResultInfo;
import com.cwf.quality.model.dto.dataanalyze.param.AnalyzerParamInfo;
import com.cwf.quality.model.dto.dataanalyze.result.RangeAnalyzeResult;
import com.cwf.quality.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class ValueRangeDataAnalyzer implements IDataAnalyzer {

    /**
     * 数值范围分析
     *
     * @param analyzerParamInfo
     * @return
     */
    @Override
    public QualityResultInfo<List<RangeAnalyzeResult>> analyzeData(AnalyzerParamInfo analyzerParamInfo) {
        SqlUtils sqlUtils = analyzerParamInfo.getSqlUtils();

        List<RangeAnalyzeResult> rangeAnalyzeResultList = new ArrayList<>();
        List<String> columnNameList = analyzerParamInfo.getColumnNameList();
        for (String columnName : columnNameList) {

            String sql = DataAnalyzerEnum.VALUE_RANGE_ANALYZE.getSql()
                    .replace("{table}", analyzerParamInfo.getTableName())
                    .replace("{column}", columnName);

            List<Map<String, Object>> mapList = sqlUtils.query4List(sql, 1);
            Long max = (Long) mapList.get(0).get("max");
            Long min = (Long) mapList.get(0).get("min");
            log.info("max:{},min:{}", max, min);
            rangeAnalyzeResultList.add(RangeAnalyzeResult.builder()
                    .columnName(columnName)
                    .min(min)
                    .max(max)
                    .build());
        }
        QualityResultInfo qualityResultInfo = QualityResultInfo.builder()
                .code(0)
                .msg("成功")
                .result(rangeAnalyzeResultList).build();
        return qualityResultInfo;
    }
}
